
require "game/ui/form/blacksmith/UIEvilStatue"

-- 雕像
function initEvilStatue(self)
    self.item:removeAllChildren();

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local pos = self.gridData:getPos();
            local grid = DungeonM.getGridByPos(pos);
            if grid.active ~= 1 then
                -- 还未激活
                tryActive(self);
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开工坊界面
            if UIMgr.getCurrentScene():isOpen("UIEvilStatue") then
                UIMgr.getCurrentScene():removeFormByName("UIEvilStatue");
            end
            local uiEvilStatue = UIEvilStatue.create(pos);
            UIMgr.getCurrentScene():addForm(uiEvilStatue);
            -- 音效
            AudioM.playFx("workshop_store");
        end
    end

    -- 工坊图标
    local class = self.gridData.classify;
    local data = EvilStatueM.queryStatue(class, 1);
    local icon = data["icon"] or "demon_statue";
    icon = getLevelItemIconPath(icon);

    self.icon = ccui.ImageView:create(icon);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
end

-- 打开激活界面
function tryActive(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local pos = self.gridData:getPos();
    if not EvilStatueM.canActiveEvilStatue(pos) then
        -- 不能激活
        self:openBuildingView(nil, 0, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
        return;
    end

    -- 激活雕像
    local function okFunc()
        DungeonActionM.go("evil_statue_active", self.gridData:getPos());

        -- 弹字
        local tip = buildingInfo["bonus_tip"];
        local list = splitString(tip, "\n");
        splitAlert(list);
    end

    -- 拒绝
    local function cancelFunc()
        -- 弹字
        local tip = buildingInfo["fail_tip"];
        local list = splitString(tip, "\n");
        splitAlert(list);
    end

    -- 可以激活
    self:openBuildingView2(okFunc, cancelFunc, buildingInfo["view_text"], buildingInfo["view_text2"]);
end